﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace MultiColumnTreeListSample
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            this.flatteningTreeView.ItemsSource = SampleProjectData.GetSampleData();

            // Bind the MultiColumnTreeList.ItemsSource property to the FlatteningTreeView.FlatItemsSource
            // to achieve multi-column tree functionality.
            // The MultiColumnTreeList always works in conjunction with a FlatteningTreeView instance, but you
            // can keep the FlatteningTreeView instance hidden in your applications.
            Binding binding = new Binding("FlatItemsSource");
            binding.Source = this.flatteningTreeView;
            this.multiColumnTreeList.SetBinding(DataGrid.ItemsSourceProperty, binding);

            // SelectedItem will be of type FlatNodeData
            binding = new Binding("SelectedItem.Data");
            binding.Source = this.multiColumnTreeList;
            binding.Converter = new TaskNameConverter();
            this.slName.SetBinding(TextBlock.TextProperty, binding);

            binding = new Binding("SelectedItem.Data");
            binding.Source = this.multiColumnTreeList;
            binding.Converter = new TaskTypeNameConverter();
            this.slType.SetBinding(TextBlock.TextProperty, binding);
        }

        private void HideTree_Click(object sender, RoutedEventArgs e)
        {
            if (this.GridSplitter1.Visibility == Visibility.Visible)
            {
                this.LayoutRoot.ColumnDefinitions[0].Width = new GridLength(0);
                this.GridSplitter1.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.LayoutRoot.ColumnDefinitions[0].Width = new GridLength(20, GridUnitType.Star);
                this.GridSplitter1.Visibility = Visibility.Visible;
            }
        }
    }


    #region CONVERTERS
    public class TaskNameConverter : IValueConverter
    {
        #region IValueConverter Members

        public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Product)
                return ((Product)value).PDName;
            else if (value is Project)
                return ((Project)value).PName;
            else if (value is Task)
                return ((Task)value).TaskName;
            else
                return String.Empty;
        }

        public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
    public class TwoWayTaskNameConverter : TaskNameConverter
    {
        private object _editingValue;
        public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            this._editingValue = value;
            return base.Convert(value, targetType, parameter, culture);
        }

        public override object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (_editingValue is Product)
                ((Product)_editingValue).PDName = (string)value;
            else if (_editingValue is Project)
                ((Project)_editingValue).PName = (string)value;
            else if (_editingValue is Task)
                ((Task)_editingValue).TaskName = (string)value;

            return _editingValue;
        }
    }

    public class DateConverter : IValueConverter
    {
        #region IValueConverter Members

        public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DateTime retVal = DateTime.MinValue;

            if (value is Product)
                retVal = ((Product)value).OverallStartTime;
            else if (value is Project)
                retVal = ((Project)value).OverallStartTime;
            else if (value is Task)
                retVal = ((Task)value).StartTime;

            if (retVal == DateTime.MinValue)
                return null;

            if (((string)parameter) == "AsText")
                return retVal.ToString("d MMM yyyy: hh tt ");
            else
                return retVal;
        }

        public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
    public class TwoWayDateConverter : DateConverter
    {
        private object _editingValue;
        public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            this._editingValue = value;
            return base.Convert(value, targetType, parameter, culture);
        }

        public override object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (_editingValue is Product)
                ((Product)_editingValue).OverallStartTime = (DateTime)value;
            else if (_editingValue is Project)
                ((Project)_editingValue).OverallStartTime = (DateTime)value;
            else if (_editingValue is Task)
                ((Task)_editingValue).StartTime = (DateTime)value;

            return _editingValue;
        }
    }

    public class TaskTypeNameConverter : IValueConverter
    {
        #region IValueConverter Members

        public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return String.Empty;
            return value.GetType().Name ;
        }

        public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
    #endregion
}
